from lxml import etree
from urllib import parse

from selenium import webdriver

def links_pro():
    param = '男装'
    driver = webdriver.PhantomJS(executable_path=r'.\browser\phantomjs.exe')
    param = parse.quote(param)
    driver.get(f'https://s.taobao.com/search?q={param}')
    n = 1
    while n <= 100:
        print('正在爬取第{}页。。'.format(n))
        page_source = driver.page_source
        html = etree.HTML(page_source)
        product_links = html.xpath("//a[@class='J_ClickStat']/@href")
        for link in product_links:
            print(link)
        try:
            driver.find_element_by_xpath("//li[@class='item next']//span[2]").click()
        except Exception:
            print('error')
        n += 1

if __name__ == '__main__':
    links_pro()
